package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.dto.ForumUserStatsDTO;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumUserStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 論壇用戶統計控制器
 */
@RestController
@RequestMapping("/api/forum/user-stats")
public class ForumUserStatsController {

    @Autowired
    private ForumUserStatsService forumUserStatsService;

    /**
     * 查詢用戶統計
     */
    @GetMapping("/{userId}")
    public CommonResponse<ForumUserStatsDTO> getUserStats(@PathVariable Integer userId) {
        try {
            ForumUserStatsDTO stats = forumUserStatsService.getUserStats(userId);
            return CommonResponse.success(stats);
        } catch (Exception e) {
            return CommonResponse.error("查詢用戶統計失敗：" + e.getMessage());
        }
    }

    /**
     * 更新用戶統計
     */
    @PutMapping("/{userId}/update")
    public CommonResponse<Boolean> updateUserStats(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.updateUserStats(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("更新用戶統計失敗：" + e.getMessage());
        }
    }

    /**
     * 增加發帖數
     */
    @PostMapping("/{userId}/increment-post")
    public CommonResponse<Boolean> incrementPostCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.incrementPostCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("增加發帖數失敗：" + e.getMessage());
        }
    }

    /**
     * 減少發帖數
     */
    @PostMapping("/{userId}/decrement-post")
    public CommonResponse<Boolean> decrementPostCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.decrementPostCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("減少發帖數失敗：" + e.getMessage());
        }
    }

    /**
     * 增加點贊數
     */
    @PostMapping("/{userId}/increment-like")
    public CommonResponse<Boolean> incrementLikeCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.incrementLikeCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("增加點贊數失敗：" + e.getMessage());
        }
    }

    /**
     * 減少點贊數
     */
    @PostMapping("/{userId}/decrement-like")
    public CommonResponse<Boolean> decrementLikeCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.decrementLikeCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("減少點贊數失敗：" + e.getMessage());
        }
    }

    /**
     * 增加評論數
     */
    @PostMapping("/{userId}/increment-comment")
    public CommonResponse<Boolean> incrementCommentCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.incrementCommentCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("增加評論數失敗：" + e.getMessage());
        }
    }

    /**
     * 減少評論數
     */
    @PostMapping("/{userId}/decrement-comment")
    public CommonResponse<Boolean> decrementCommentCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.decrementCommentCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("減少評論數失敗：" + e.getMessage());
        }
    }

    /**
     * 增加關注數
     */
    @PostMapping("/{userId}/increment-following")
    public CommonResponse<Boolean> incrementFollowingCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.incrementFollowingCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("增加關注數失敗：" + e.getMessage());
        }
    }

    /**
     * 減少關注數
     */
    @PostMapping("/{userId}/decrement-following")
    public CommonResponse<Boolean> decrementFollowingCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.decrementFollowingCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("減少關注數失敗：" + e.getMessage());
        }
    }

    /**
     * 增加粉絲數
     */
    @PostMapping("/{userId}/increment-follower")
    public CommonResponse<Boolean> incrementFollowerCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.incrementFollowerCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("增加粉絲數失敗：" + e.getMessage());
        }
    }

    /**
     * 減少粉絲數
     */
    @PostMapping("/{userId}/decrement-follower")
    public CommonResponse<Boolean> decrementFollowerCount(@PathVariable Integer userId) {
        try {
            boolean result = forumUserStatsService.decrementFollowerCount(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("減少粉絲數失敗：" + e.getMessage());
        }
    }
} 